package com.yunshang.shoppingmall.controller.User;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunshang.shoppingmall.common.HttpRequest;
import com.yunshang.shoppingmall.common.LayUIUtil;
import com.yunshang.shoppingmall.common.Pwd;
import com.yunshang.shoppingmall.common.layuiJson;
import com.yunshang.shoppingmall.entity.User;
import com.yunshang.shoppingmall.entity.UserNo;
import com.yunshang.shoppingmall.entity.UserRank;
import com.yunshang.shoppingmall.service.SysUserNoService;
import com.yunshang.shoppingmall.service.User.UserNoService;
import com.yunshang.shoppingmall.service.User.UserRankService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserNoController {

//    @Autowired
//    UserNoService userNoService;

    @Autowired
    SysUserNoService sysUserNoService;

    @Autowired
    UserRankService userRankService;

    //会员编号列表页面
    @RequestMapping("/user/getUserNoList")
    public String getUserNoList() {
        return "user/UserNoList";
    }

    //会员编号列表Json数据
    @ResponseBody
    @RequestMapping("/user/JsonUserNoList")
    public LayUIUtil JsonUserNoList(HttpServletRequest request, String userNo, String name) {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        int pages = 0;
        int limits = 10;
        if(page!=null&&page!=""){pages=Integer.parseInt(page);}
        if(limit!=null&&limit!=""){limits=Integer.parseInt(limit);}
        PageHelper.startPage(pages, limits);
        List<UserNo> userNoList=sysUserNoService.selectUserNoList();
        int s=userNoList.get(0).getRank();

       PageInfo<UserNo> userNoPageInfo=new PageInfo<>(userNoList);
        int count=0;
        count=(int)userNoPageInfo.getTotal();
        return LayUIUtil.data(count,userNoList);
    }

    //会员编号添加页面
    @RequestMapping("/user/getUserNoAdd")
    public ModelAndView getUserNoAdd() {
        ModelAndView modelAndView = new ModelAndView("user/UserNoAdd");
        List<UserRank> userRankList = userRankService.selectUserRankList();
        modelAndView.addObject(userRankList);
        return modelAndView;
    }

    //增加会员编号
    @ResponseBody
    @RequestMapping(value = "/user/addUserNo")
    @Transactional
    public Map<String, Object> addUserNo(@RequestParam("userNo") String userNo1,
                                         @RequestParam("name") String name,
                                         @RequestParam("rank") int rank,
                                         @RequestParam("recommend") int recommend,
                                         @RequestParam("sort") int sort){
        Logger logger = LoggerFactory.getLogger(UserNoController.class);

        Map<String, Object> map = new HashMap<>();
        if (userNo1 != "") {
            UserNo userNo = new UserNo();
            userNo.setUserNo(userNo1);
            userNo.setName(name);
            userNo.setRank(rank);
            userNo.setRecommend(recommend);
            userNo.setSort(sort);
            userNo.setAngel(0);
            userNo.setPwd(Pwd.GetPwd("123456",""));
            logger.info(userNo.toString());
            int rows =0;// userNoService.insert(userNo);
            if (rows > 0) {
                map.put("result", rows);
                return map;
            } else {
                map.put("result", "添加失败！");
                return map;
            }

        } else {
            map.put("result", "添加的值不能为空！");
            return map;
        }
    }

//    //根据等级ID查询会员信息
//    @RequestMapping("/user/getUserNoShow")
//    public ModelAndView getUserNoShow(Integer id) {
//        ModelAndView modelAndView = new ModelAndView("user/UserNoShow");
//        //查询列表
//        int adId = Integer.valueOf(id);
//        UserNo userNo = userNoService.selectByPrimaryKey(adId);
//
//        modelAndView.addObject(userNo);
//        return modelAndView;
//    }
//

    @ResponseBody
    @RequestMapping("/user/seachUserNoListByUserNoOrName")
    public LayUIUtil seachUserNoListByUserNoOrName(@RequestParam("name") String name,@RequestParam("userNo") String userNo){
        if(name==null&&name==""&&userNo==null&&userNo==""){return null;}
        if(name==""){
            name=null;
        }
        if(userNo==""){
            userNo=null;
        }
        UserNo userNoEntity=new UserNo();
        userNoEntity.setName(name);
        userNoEntity.setUserNo(userNo);
       List<UserNo> userNoList=sysUserNoService.selectUserListByUserNoOrName(userNoEntity);
        PageInfo<UserNo> pageInfo=new PageInfo<>(userNoList);
        int count=0;
        count=(int)pageInfo.getTotal();
        return LayUIUtil.data(count,userNoList);
    }


    //根据编号ID查询会员信息
    @RequestMapping("/user/getUserNoEdit")
    public ModelAndView getUserNoEdit(@RequestParam("id") String id) {
        ModelAndView modelAndView = new ModelAndView("user/UserNoShow");
        //查询列表
        int ids=Integer.parseInt(id);
        UserNo userNo =sysUserNoService.selectByPrimaryKey(ids);
        System.out.println("ids="+ids);
        modelAndView.addObject("userNo",userNo);
        return modelAndView;
    }
//
   //编辑会员编号
    @RequestMapping("/openEditUserNo")
  public  ModelAndView openEditUserNo(@RequestParam("id")String id ){
        ModelAndView modelAndView=new ModelAndView("user/UserNoEdit");
        int ids=Integer.parseInt(id);
        UserNo userNo=sysUserNoService.selectByPrimaryKey(ids);
        System.out.println(userNo.getName());
        modelAndView.addObject("userNo",userNo);
        return  modelAndView;
  }

 //保存编辑会员编号的数据
    @ResponseBody
    @RequestMapping(value = "/saveEditUserNo",method = RequestMethod.POST)
 public Map<String,Object> saveEditUserNo(@RequestParam Map<String,String> userNoMap){
        Map<String,Object> map=new HashMap<>();
        UserNo userNo=new UserNo();
        userNo.setName(userNoMap.get("name"));
        userNo.setId(Integer.parseInt(userNoMap.get("id")));
        userNo.setUserNo(userNoMap.get("userNo"));
        userNo.setSort(Integer.parseInt(userNoMap.get("sort")));
        int row=sysUserNoService.updateByPrimaryKeySelective(userNo);

        if(row>0){
            map.put("msg","1");
        }

        return map;
 }



//    //根据Id删除会员
    @ResponseBody
    @RequestMapping("delUserNo")
    @Transactional
    public Map<String,Object> delUserNo(String id){
        Map<String,Object> map=new HashMap<>();
            int ids=0;
            if(id!=null&&id!=""){
               ids =Integer.parseInt(id);
            }
            int row =sysUserNoService.deleteByPrimaryKey(ids);
            if(row>0){
                map.put("msg","1");
            }else{
                map.put("msg","0");
            }
            return map;
    }
}